package com.xuecheng.content.controller;

import com.xuecheng.commons.model.vo.CoursePubVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.model.vo.WorkCountVo;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.content.domain.CoursePub;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.content.service.CoursePubService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程发布前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
public class CoursePubController {

    @Autowired
    private CoursePubService  coursePubService;

    //课程预览
    @GetMapping("/coursePub/preview/{courseBaseId}")
    public ModelAndView preview(@PathVariable("courseBaseId")Long courseBaseId){
        //获取model
        Map model = coursePubService.preview(courseBaseId);
        //输出模型
        return new ModelAndView("learing_article",model);
    }

    //课程发布
    @PostMapping("/coursePub/publish/{courseBaseId}")
    public ResponseResult publish(@PathVariable("courseBaseId")Long courseBaseId) throws Exception {
        return coursePubService.publish(courseBaseId);
    }

    //根据id查询课程发布对象
    @GetMapping("/coursePub/{id}")
    public ResponseResult<CoursePubVo> findById(@PathVariable("id")Long id){
        //获取查询结果
        CoursePub coursePub = coursePubService.getById(id);
        //转为vo
        CoursePubVo coursePubVo = BeanHelper.copyProperties(coursePub, CoursePubVo.class);
        //返回
        return ResponseResult.okResult(coursePubVo);
    }

    //根据课程名返回id集合
    @GetMapping("/coursePub/getName")
    public ResponseResult<List<Long>> findIdByName(@RequestParam String courseName){
        //获取查询结果
        List<CoursePub> coursePubList = coursePubService.lambdaQuery().like(CoursePub::getName, courseName).list();
        List<Long> coursePubIdList=new ArrayList<>();
        for (CoursePub coursePub : coursePubList) {
            coursePubIdList.add(coursePub.getId());
        }
        return ResponseResult.okResult(coursePubIdList);
    }
}
